package com.ruoyi.web.controller.business;

import com.ruoyi.business.bo.HcxMerchantOrderAddBo;
import com.ruoyi.business.bo.HcxMerchantOrderEditBo;
import com.ruoyi.business.bo.HcxMerchantOrderQueryBo;
import com.ruoyi.business.service.IHcxMerchantOrderService;
import com.ruoyi.business.vo.HcxMerchantOrderVo;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 商家订单Controller
 *
 * @author wsdz
 * @date 2022-08-17
 */
@Api(value = "商家订单控制器", tags = {"商家订单管理"})
@AllArgsConstructor
@RestController
@RequestMapping("/business/MerchantOrder")
public class HcxMerchantOrderController extends BaseController {

    private final IHcxMerchantOrderService iHcxMerchantOrderService;

    /**
     * 查询商家订单列表
     */
    @ApiOperation("查询商家订单列表")
    @PreAuthorize("@ss.hasPermi('business:MerchantOrder:list')")
    @GetMapping("/list")
    public TableDataInfo<HcxMerchantOrderVo> list(@Validated HcxMerchantOrderQueryBo bo) {
        return iHcxMerchantOrderService.queryPageList(bo);
    }

    /**
     * 导出商家订单列表
     */
    @ApiOperation("导出商家订单列表")
    @PreAuthorize("@ss.hasPermi('business:MerchantOrder:export')")
    @Log(title = "商家订单", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<Void> export(@Validated HcxMerchantOrderQueryBo bo) {
        List<HcxMerchantOrderVo> list = iHcxMerchantOrderService.queryList(bo);
        ExcelUtil<HcxMerchantOrderVo> util = new ExcelUtil<>(HcxMerchantOrderVo.class);
        return util.exportExcel(list, "商家订单");
    }

    /**
     * 获取商家订单详细信息
     */
    @ApiOperation("获取商家订单详细信息")
    @PreAuthorize("@ss.hasPermi('business:MerchantOrder:query')")
    @GetMapping("/{id}")
    public AjaxResult<HcxMerchantOrderVo> getInfo(@NotNull(message = "主键不能为空")
                                                  @PathVariable("id") Long id) {
        return AjaxResult.success(iHcxMerchantOrderService.queryById(id));
    }

    /**
     * 新增商家订单
     */
    @ApiOperation("新增商家订单")
    @PreAuthorize("@ss.hasPermi('business:MerchantOrder:add')")
    @Log(title = "商家订单", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping()
    public AjaxResult<Void> add(@Validated @RequestBody HcxMerchantOrderAddBo bo) {
        return toAjax(iHcxMerchantOrderService.insertByAddBo(bo) ? 1 : 0);
    }

    /**
     * 修改商家订单
     */
    @ApiOperation("修改商家订单")
    @PreAuthorize("@ss.hasPermi('business:MerchantOrder:edit')")
    @Log(title = "商家订单", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping()
    public AjaxResult<Void> edit(@Validated @RequestBody HcxMerchantOrderEditBo bo) {
        return toAjax(iHcxMerchantOrderService.updateByEditBo(bo) ? 1 : 0);
    }

    /**
     * 删除商家订单
     */
    @ApiOperation("删除商家订单")
    @PreAuthorize("@ss.hasPermi('business:MerchantOrder:remove')")
    @Log(title = "商家订单" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")
                                       @PathVariable Long[] ids) {
        return toAjax(iHcxMerchantOrderService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }

    /**
     * 导入商家订单数据
     */
    @ApiOperation("导入商家订单数据")
    @ApiImplicitParam(name = "file", value = "文件", required = true, dataType = "file")
    @PreAuthorize("@ss.hasPermi('business:MerchantOrder:import')")
    @Log(title = "商家订单" , businessType = BusinessType.IMPORT)
    @PostMapping("/import")
    public AjaxResult<Void> importData(MultipartFile file) {

        ExcelUtil<HcxMerchantOrderVo> util = new ExcelUtil<>(HcxMerchantOrderVo.class);
        List<HcxMerchantOrderVo> hcxMerchantOrderList;
        try {
            hcxMerchantOrderList = util.importExcel(file.getInputStream());
        } catch (Exception e) {
            e.printStackTrace();
            throw new CustomException("文件解析异常！message：" + e.getMessage());
        }
        String message = iHcxMerchantOrderService.importHcxMerchantOrder(hcxMerchantOrderList);
        return AjaxResult.success(message);

    }

    /**
     * 下载模板
     *
     * @return AjaxResult 模板文件
     */
    @ApiOperation("下载模板")
    @GetMapping("/importTemplate")
    public AjaxResult importTemplate() {
        ExcelUtil<HcxMerchantOrderVo> util = new ExcelUtil<>(HcxMerchantOrderVo.class);
        return util.importTemplateExcel("商家订单数据");
    }
}
